import Store from "@/store";
import LivePlayerObj from "@/assets/js/player/LivePlayerObj";


let config=Store.state.singleGameConfig;
export default class SelfLivePlayerObj extends LivePlayerObj{

    constructor(context,x,y,radius,speed,backgroundImage,color) {
        super(context,x,y,radius,speed,backgroundImage,color);
    }
    start(){
        this.addListeningEvents();
    }

    addListeningEvents(){
        let that=this;
        this.ctx.canvas.onmousedown=(e)=>{
            const rect=this.ctx.canvas.getBoundingClientRect();
            if(e.which===3){
                that.moveTo((e.clientX-rect.left)/Store.state.scale,(e.clientY-rect.top)/Store.state.scale);
            }else if(e.which===1){
                if(that.curSkill==="fireBall"){
                    this.shootFireball((e.clientX-rect.left)/Store.state.scale,(e.clientY-rect.top)/Store.state.scale);
                }
                that.curSkill=null;
            }
        }
        document.onkeydown=(e)=>{
            if(this.spendTime>config.fight.spendTime){
                if(that.curSkill!==null){
                    that.curSkill=null;
                    return false;
                }
                if(e.which===81) { // q
                    that.curSkill="fireBall";
                    return false;
                }
            }
        }
    }
    onDestroy() {
        document.onkeydown=()=>false;
        super.onDestroy();
    }
    render() {
        super.render();
        let that=this;
        // 如果有技能，就渲染技能范围框
        if(this.curSkill){
            let scale=Store.state.scale;
            this.ctx.beginPath();
            let radius=eval("config.skill."+that.curSkill+".moveLength")*scale;
            this.ctx.arc(this.x*Store.state.scale,this.y*Store.state.scale,radius,0,2*Math.PI);
            this.ctx.strokeStyle=config.skill.rangeColor;
            this.ctx.stroke();
        }
    }
}